package com.shiwaixiangcun.customer.ui.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.shiwaixiangcun.customer.GlobalApi
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.base.BaseActivity
import com.shiwaixiangcun.customer.event.EventUtil
import com.shiwaixiangcun.customer.helper.UpdateHelper
import com.shiwaixiangcun.customer.ui.dialog.DialogProgress
import com.shiwaixiangcun.customer.update.UpdateAppManager
import com.shiwaixiangcun.customer.utils.OkGoUpdateHttpUtil
import kotlinx.android.synthetic.main.activity_for_life.*
import kotlinx.android.synthetic.main.top_bar_white.*
import java.util.*

/**
 * 关于页面
 * @author Administrator
 */
class AboutActivity : BaseActivity(), View.OnClickListener {

    private var versionCode: Int = -1
    private var versionName: String? = null


    private var mDialog: DialogProgress? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_for_life)
        layoutView()

    }

    /***
     * init  View
     */
    @SuppressLint("SetTextI18n")
    private fun layoutView() {
        mDialog = DialogProgress(this)
        versionName = UpdateHelper.getInstance().getVersionName(this)
        versionCode = UpdateHelper.getInstance().getVersionCode(this)
        tv_version.text = "v $versionName"
        tv_page_name.text = "关于世外生活"
        back_left.setOnClickListener(this)
        rlayout_feedback.setOnClickListener(this)
        rlayout_privacy.setOnClickListener(this)
        rlayout_update.setOnClickListener(this)
    }

    override fun onClick(view: View) {
        val id = view.id
        when (id) {
            R.id.back_left -> finish()
            R.id.rlayout_feedback -> readyGo(FeedBackActivity::class.java)
            R.id.rlayout_privacy -> readyGo(PrivacyActivity::class.java)
            R.id.rlayout_update -> checkAndUpdate()
        }
    }


    /**
     * 检测更新
     */
    private fun checkAndUpdate() {
        val params = HashMap<String, String>(2)
        params.put("appType", "OWNER_APP")
        params.put("version", versionCode.toString())
        UpdateAppManager.Builder()
                //当前Activity
                .setActivity(this)
                .setParams(params)
                //更新地址
                .setUpdateUrl(GlobalApi.appUpdate)
                //实现httpManager接口的对象
                .setHttpManager(OkGoUpdateHttpUtil())
                .build()
                .update()

    }

//
//    @Subscribe(priority = 1, threadMode = ThreadMode.POSTING)
//    fun updateUI(simpleEvent: SimpleEvent) {
//        if (simpleEvent.mEventType != SimpleEvent.UPDATE_VERSION) {
//            return
//        }
//        when (simpleEvent.mEventValue) {
//            1 -> {
//                val versionBean = simpleEvent.data as VersionBean
//                showUpdateDialog(versionBean.latestVersion, versionBean.downloadUrl, "")
//                EventUtil.getInstance().cancelEventDelivery(simpleEvent)
//            }
//            else -> {
//                showToastShort("当前已是最新版本!")
//                EventUtil.getInstance().cancelEventDelivery(simpleEvent)
//            }
//        }
//
//    }


    override fun onDestroy() {
        EventUtil.getInstance().unregister(this)
        super.onDestroy()
    }


}
